import random

from django.test import TestCase

# Create your tests here.
#单元测试
from faker import Faker

from apitest.models import UserInfo, Author, AuthorDetail, Publisher, Book
from apitest.models import Subject


class UnitTest(TestCase):
    def test_add(self):
        UserInfo.objects.create(name='test1', password='123456')
        UserInfo.objects.create(name='test2', password='123456')
        UserInfo.objects.create(name='test3', password='123456')

        user = UserInfo(name='test4',password='123456')
        user.save()

        self.assertEqual(UserInfo.objects.filter(name__icontains='test').count(), 4)
        alluser = UserInfo.objects.all()
        for user in alluser:
            print(user.name)

    def test_Author(self):

        faker = Faker('zh_CN')
        Author.objects.create(author_name='lisi')
        author = Author.objects.create(author_name='zhangsan')
        #修改
        author.author_name='zhangsan2'
        author.save()
        sex = random.choice([0, 1])
        AuthorDetail.objects.create(author_sex=sex, author=author,
                                    author_phone=faker.phone_number(),author_age=random.randint(30,80))
        all_aut= Author.objects.all()
        for aut in all_aut:
            print(aut.author_name)

        #级联
        #author.delete()
        all_detail = AuthorDetail.objects.all()
        for aut in all_detail:
            print(aut.author_phone,aut.author.author_name)

    def test_Publisher(self):
        faker = Faker('zh_CN')
        publisher =Publisher.objects.create(publisher_name='publisher1',publisher_city=faker.city(),
                                 publisher_address=faker.address(),publisher_website=faker.url())

        Book.objects.create(book_name='book1',price=random.randint(10,100),publisher=publisher)
        Book.objects.create(book_name='book2', price=random.randint(10, 100), publisher=publisher)
        Book.objects.create(book_name='book3', price=random.randint(10, 100), publisher=publisher)

        all_book = Book.objects.all()
        for book in all_book:
            print(book.book_name,book.price,book.publisher.publisher_name)

    def test_Book(self):
        faker = Faker('zh_CN')
        publisher = Publisher.objects.create(publisher_name='publisher1', publisher_city=faker.city(),
                                             publisher_address=faker.address(), publisher_website=faker.url())

        author1 = Author.objects.create(author_name='lisi')
        author2 = Author.objects.create(author_name='zhangsan')
         #多对多关联
        book1= Book.objects.create(book_name='book1', price=random.randint(10, 100), publisher=publisher)
        book1.author.add(author1)
        book1.author.add(author2)
        # Book.objects.create(book_name='book2', price=random.randint(10, 100), publisher=publisher)
        # Book.objects.create(book_name='book3', price=random.randint(10, 100), publisher=publisher)
        all_book = Book.objects.all()
        for book in all_book:
            print(book.book_name, book.price, book.publisher.publisher_name)
            print(len(book.author.all()))
            for aut in book.author.all():
                print(aut.author_name)

    def test_subject(self):
        Subject.objects.create(name='python')
        Subject.objects.create(name='java')
        Subject.objects.create(name='c++')
        all_subject = Subject.objects.all()
        for sub in all_subject:
            print(sub.name)

